package com.pai4j.zwiki.controller;

import com.pai4j.zwiki.domain.entity.DetailedAnalysisReportEntity;
import com.pai4j.zwiki.service.ProjectAnalysisService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 详细报告控制器
 * 提供详细代码分析报告的查看接口
 *
 * @author pai4j
 * @since 2025-10-12
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/report")
@RequiredArgsConstructor
public class DetailedReportController {

    private final ProjectAnalysisService projectAnalysisService;

    /**
     * 获取详细分析报告
     */
    @GetMapping("/detailed/{taskId}")
    public ResponseEntity<Map<String, Object>> getDetailedReport(@PathVariable String taskId) {
        log.info("查询详细分析报告: {}", taskId);
        
        try {
            DetailedAnalysisReportEntity report = projectAnalysisService.getDetailedReport(taskId);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", "0");
            result.put("message", "查询成功");
            result.put("data", report);
            
            return ResponseEntity.ok(result);
            
        } catch (Exception e) {
            log.error("查询详细报告失败", e);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", "404");
            result.put("message", "报告不存在");
            
            return ResponseEntity.status(404).body(result);
        }
    }

    /**
     * 获取综合分析报告（Markdown格式）
     */
    @GetMapping("/comprehensive/{taskId}")
    public ResponseEntity<Map<String, Object>> getComprehensiveReport(@PathVariable String taskId) {
        log.info("查询综合分析报告: {}", taskId);
        
        try {
            DetailedAnalysisReportEntity report = projectAnalysisService.getDetailedReport(taskId);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", "0");
            result.put("message", "查询成功");
            result.put("data", Map.of(
                "taskId", taskId,
                "report", report.getComprehensiveReport(),
                "createdAt", report.getCreatedAt()
            ));
            
            return ResponseEntity.ok(result);
            
        } catch (Exception e) {
            log.error("查询综合报告失败", e);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", "404");
            result.put("message", "报告不存在");
            
            return ResponseEntity.status(404).body(result);
        }
    }

    /**
     * 获取报告中的所有图表路径
     */
    @GetMapping("/diagrams/{taskId}")
    public ResponseEntity<Map<String, Object>> getDiagrams(@PathVariable String taskId) {
        log.info("查询报告图表: {}", taskId);
        
        try {
            DetailedAnalysisReportEntity report = projectAnalysisService.getDetailedReport(taskId);
            
            Map<String, String> diagrams = new HashMap<>();
            diagrams.put("detailedClassDiagram", report.getDetailedClassDiagramPath());
            diagrams.put("packageDependency", report.getPackageDependencyPath());
            diagrams.put("callChain", report.getCallChainPath());
            diagrams.put("layerArchitecture", report.getLayerArchitecturePath());
            diagrams.put("moduleRelation", report.getModuleRelationPath());
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", "0");
            result.put("message", "查询成功");
            result.put("data", diagrams);
            
            return ResponseEntity.ok(result);
            
        } catch (Exception e) {
            log.error("查询图表失败", e);
            
            Map<String, Object> result = new HashMap<>();
            result.put("code", "404");
            result.put("message", "图表不存在");
            
            return ResponseEntity.status(404).body(result);
        }
    }
}

